import type {IFoodItem} from "../types/IFoodItem.ts";
import type {IFoodListService} from "../types/IFoodListService.ts";
import {reactive, ref, watch} from "vue";


export class FoodListService implements IFoodListService {
    order:Object={
        list: [
            {
                id: 0,
                name: "纯肉鲜肉（水饺）",
                description: "肉馅",
                quantity: 0,
                price: 15,
                img: "2023/10/23/5893D86C58920F6ADDE5EAC9505582F5",
            },
            {
                id: 1,
                name: "玉米鲜肉（水饺）",
                description: "玉米",
                quantity: 0,
                price: 15,
                img: "2023/10/23/EEB422FAAC285B1ADFDF203345D37F61"
            },
            {
                id: 2,
                name: "牛肉鲜肉（水饺）",
                description: "牛肉",
                quantity: 0,
                price: 15,
                img: "2023/10/23/F596B61E275EAE441CABB5CC1B9F0CDD",
            },
        ],
        totalQuantity : ref(0),
        totalPrice : ref(0),

    }
    // #list: IFoodItem[] = [
    //     {
    //         id: 0,
    //         name: "纯肉鲜肉（水饺）",
    //         description: "肉馅",
    //         quantity: 0,
    //         price: 15,
    //         img: "2023/10/23/5893D86C58920F6ADDE5EAC9505582F5",
    //     },
    //     {
    //         id: 1,
    //         name: "玉米鲜肉（水饺）",
    //         description: "玉米",
    //         quantity: 0,
    //         price: 15,
    //         img: "2023/10/23/EEB422FAAC285B1ADFDF203345D37F61"
    //     },
    //     {
    //         id: 2,
    //         name: "牛肉鲜肉（水饺）",
    //         description: "牛肉",
    //         quantity: 0,
    //         price: 15,
    //         img: "2023/10/23/F596B61E275EAE441CABB5CC1B9F0CDD",
    //     },
    // ];
    // totalQuantity = ref(0);
    // totalPrice = ref(0);

    constructor() {
        this.order = reactive(this.loadFromLocalStorage());

        watch(this.order, () => {
            this.saveToLocalStorage();
            this.order.totalQuantity = this.order.list.reduce(
                (total, item) => total + item.quantity,
                0
            );
            this.order.totalPrice = this.order.list.reduce(
                (total, item) => total + item.quantity * item.price,
                0
            );
            console.log("list 被修改");
        })
    }


    getList(): IFoodItem[] {
        return this.order.list;
    }

    loadFromLocalStorage(): IFoodItem[] {
        const strInStorage = localStorage.getItem('order');

        if (strInStorage != null) {
            return JSON.parse(strInStorage);
            console.log("###" + strInStorage);

        }
        console.log("!!!" + strInStorage);
        return this.order; // 如果没有本地存储数据，则返回默认列表
    }


    saveToLocalStorage(): boolean {
        localStorage.setItem("order",JSON.stringify(this.order))
        return true;
    }

    addQuantity(id: number) {
        this.order.list[id].quantity++;
    }

    minusQuantity(id: number) {
        this.order.list[id].quantity--;
    }

    getPrice(): number {
        return this.order.totalPrice
    }

    getQuantity(): number {
        return this.order.totalQuantity;
    }
}
